package com.lagou.edu.support;

import com.alibaba.druid.pool.DruidDataSource;
import com.lagou.edu.annotation.Component;
import com.lagou.edu.annotation.Service;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

// @Component(value="connectionUtil")
@Component
public class ConnectionUtil {

    private static DruidDataSource dataSource = new DruidDataSource();

    static {
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://server.aliyun:16033/lg");
        dataSource.setUsername("root");
        dataSource.setPassword("141535");
    }
    /**
     *
     */
    public ThreadLocal<Connection> currentConnection = new ThreadLocal<>();

    public Connection getCurrentConnection() throws SQLException {
        if (null == currentConnection.get()) {
            Connection connection = dataSource.getConnection();
            currentConnection.set(connection);
            return connection;
        }
        return currentConnection.get();
    }

}
